package drds.data_propagate.binlog_event_filter;

import com.google.common.base.Function;
import com.google.common.collect.MapMaker;
import com.google.common.collect.MigrateMap;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.Perl5Compiler;

import java.util.Map;

public class PatternUtils {


    private static Map<String, Pattern> patternStringToPatternMap = MigrateMap.makeComputingMap(new MapMaker().softValues(),
            new Function<String, Pattern>() {

                public Pattern apply(String patternString) {
                    try {
                        PatternCompiler perl5Compiler = new Perl5Compiler();
                        return perl5Compiler.compile(patternString, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
                    } catch (MalformedPatternException e) {
                        throw new RuntimeException(e);
                    }
                }
            });

    public static Pattern getPattern(String pattern) {
        return patternStringToPatternMap.get(pattern);
    }


}
